R Workflow
  • D. Palleschi
  1. 2  Troubleshooting
  • 1  Preface
  • 2  Troubleshooting
  • 3  Git set-up
  • 4  Quarto
  • 5  R Packages for a Reproducibile Workflow
  • 6  UpdateR and (re-)Install Packages
  • 7  Zotero
  • 8  Docker

Table of contents

  • 3 Rmarkdown knitting PDF
    • 3.1 Solution
  • 4 Rmarkdown and kableExtra with R version 4.3
  • 5 Stargazer
    • 5.1 Solution
  • 6 31.08.2022: updated to R version 4.2.1 (2022-06-23), now stargazer won’t compile tables where model names are too long
  • 7 Solution: discussed here https://www.reddit.com/r/rstats/comments/ucmtdn/issue_with_stargazer_package_after_update_to_r_420/
  • 8 code below from linked solution https://gist.github.com/alexeyknorre/b0780836f4cec04d41a863a683f91b53
    • 8.1 Quick fix for stargazer <= 5.2.3 is.na() issue with long model names in R >= 4.2
  • 9 Unload stargazer if loaded

2  Troubleshooting

Author

Daniela Palleschi

Published

July 8, 2023

library(knitr)
hook_output = knit_hooks$get('output')
knit_hooks$set(output = function(x, options) {
  # this hook is used only when the linewidth option is not NULL
  if (!is.null(n <- options$linewidth)) {
    x = xfun::split_lines(x)
    # any lines wider than n should be wrapped
    if (any(nchar(x) > n)) x = strwrap(x, width = n)
    x = paste(x, collapse = '\n')
  }
  hook_output(x, options)
})

In this document I record errors that I encounter and how I resolved them. Solutions will typically be from Googling, relevant links will be provided.

3 Rmarkdown knitting PDF

Error message:

tlmgr update --all --self
fmtutil [ERROR]: format directory not writable: /usr/local/texlive/2019/texmf-var/web2c
tlmgr: package repository https://mirror.clientvps.com/CTAN/systems/texlive/tlnet (not verified: gpg unavailable)
tlmgr install: package already present: l3kernel
! LaTeX Error: Mismatched LaTeX support files detected.
(LaTeX)        Loading 'expl3.sty' aborted!
(LaTeX)        
(LaTeX)        The L3 programming layer in the LaTeX format
(LaTeX)        is dated 2023-02-01, but in your TeX tree the files require
(LaTeX)        at least 2023-02-02.

Error: LaTeX failed to compile 03-exp123.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See 03-exp123.log for more info.
In addition: Warning messages:
1: Removed 156 rows containing non-finite values (stat_ydensity). 
2: Removed 156 rows containing non-finite values (stat_boxplot). 
3: Removed 156 rows containing non-finite values (stat_summary). 
4: Removed 156 rows containing non-finite values (stat_summary). 
5: Removed 156 rows containing non-finite values (stat_ydensity). 
6: Removed 156 rows containing non-finite values (stat_boxplot). 
7: Removed 156 rows containing non-finite values (stat_summary). 
8: Removed 156 rows containing non-finite values (stat_summary). 
Execution halted

In the Terminal, I type

kpsewhich --all --engine=pdftex pdflatex.fmt

And get:

(base) administrators-MacBook-3:Lifetime Project danielapalleschi$ kpsewhich --all --engine=pdftex pdflatex.fmt
/usr/local/texlive/2019/texmf-var/web2c/pdftex/pdflatex.fmt

So my pdftex version is from 2019 or soemthing.

In the Terminal, I typed:

sudo fmtutil-sys --all

And entered my laptop’s administrator Password when prompted (it won’t show that you’re typing, but just type the password and hit Enter). Then Terminal was running for a couple of minutes, and ended with the output:

Transcript written on mex.log.
fmtutil [INFO]: /usr/local/texlive/2019/texmf-var/web2c/pdftex/mex.fmt installed.
fmtutil [INFO]: Disabled formats: 6
fmtutil [INFO]: Successfully rebuilt formats: 45
fmtutil [INFO]: Total formats: 51
fmtutil [INFO]: exiting with status 0

When I went to knit a PDF I still got the same errors.

Then I ran

fmtutil-user --all

No password required, it ran again for a few minutes. At the end, I got

Transcript written on mf.log.
fmtutil [INFO]: /Users/danielapalleschi/Library/texlive/2019/texmf-var/web2c/metafont/mf.base installed.
fmtutil [INFO]: Disabled formats: 6
fmtutil [INFO]: Successfully rebuilt formats: 45
fmtutil [INFO]: Total formats: 51
fmtutil [INFO]: exiting with status 0

3.1 Solution

In the end I went into my Time Machine (external harddrive) to a point right before this problem started happening, and restored the ‘Macintosh HD/Library’ folder, because presumably something happened to the TexLive file or something in there. That solved the problem.

4 Rmarkdown and kableExtra with R version 4.3

5 Stargazer

5.1 Solution

6 31.08.2022: updated to R version 4.2.1 (2022-06-23), now stargazer won’t compile tables where model names are too long

7 Solution: discussed here https://www.reddit.com/r/rstats/comments/ucmtdn/issue_with_stargazer_package_after_update_to_r_420/

8 code below from linked solution https://gist.github.com/alexeyknorre/b0780836f4cec04d41a863a683f91b53

8.1 Quick fix for stargazer <= 5.2.3 is.na() issue with long model names in R >= 4.2

9 Unload stargazer if loaded

detach(“package:stargazer”,unload=T) # Delete it remove.packages(“stargazer”) # Download the source download.file(“https://cran.r-project.org/src/contrib/stargazer_5.2.3.tar.gz”, destfile = “stargazer_5.2.3.tar.gz”) # Unpack untar(“stargazer_5.2.3.tar.gz”) # Read the sourcefile with .inside.bracket fun stargazer_src <- readLines(“stargazer/R/stargazer-internal.R”) # Move the length check 5 lines up so it precedes is.na(.) stargazer_src[1990] <- stargazer_src[1995] stargazer_src[1995] <- “” # Save back writeLines(stargazer_src, con=“stargazer/R/stargazer-internal.R”) # Compile and install the patched package install.packages(“stargazer”, repos = NULL, type=“source”) ### FROM NOW ON: shorter model names for stargazer

`

1  Preface
3  Git set-up
Source Code
---
title: "Troubleshooting"
author: "Daniela Palleschi"
date: "`r Sys.Date()`"
format: 
  html:
    toc: true
    number-sections: true
    code-overflow: wrap
  pdf:
    output-file: troubleshooting.pdf
    toc: true
    toc-depth: 2
    number-sections: true
    code-link: true
    code-overflow: wrap
    code-tools: true
    self-contained: true
---

```{r}
knitr::opts_chunk$set(eval = F, # change this to 'eval = T' to reproduce the analyses; make sure to comment out 
                      echo = T, # 'print code chunk?'
                      message = F, # 'print messages (e.g., warnings)?'
                      error = F,
                      warning = F)
```

```{r wrap-hook}
library(knitr)
hook_output = knit_hooks$get('output')
knit_hooks$set(output = function(x, options) {
  # this hook is used only when the linewidth option is not NULL
  if (!is.null(n <- options$linewidth)) {
    x = xfun::split_lines(x)
    # any lines wider than n should be wrapped
    if (any(nchar(x) > n)) x = strwrap(x, width = n)
    x = paste(x, collapse = '\n')
  }
  hook_output(x, options)
})
```

In this document I record errors that I encounter and how I resolved them. Solutions will typically be from Googling, relevant links will be provided.


# Rmarkdown knitting PDF

Error message:

```{r, echo = T, eval = F}
tlmgr update --all --self
fmtutil [ERROR]: format directory not writable: /usr/local/texlive/2019/texmf-var/web2c
tlmgr: package repository https://mirror.clientvps.com/CTAN/systems/texlive/tlnet (not verified: gpg unavailable)
tlmgr install: package already present: l3kernel
! LaTeX Error: Mismatched LaTeX support files detected.
(LaTeX)        Loading 'expl3.sty' aborted!
(LaTeX)        
(LaTeX)        The L3 programming layer in the LaTeX format
(LaTeX)        is dated 2023-02-01, but in your TeX tree the files require
(LaTeX)        at least 2023-02-02.

Error: LaTeX failed to compile 03-exp123.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See 03-exp123.log for more info.
In addition: Warning messages:
1: Removed 156 rows containing non-finite values (stat_ydensity). 
2: Removed 156 rows containing non-finite values (stat_boxplot). 
3: Removed 156 rows containing non-finite values (stat_summary). 
4: Removed 156 rows containing non-finite values (stat_summary). 
5: Removed 156 rows containing non-finite values (stat_ydensity). 
6: Removed 156 rows containing non-finite values (stat_boxplot). 
7: Removed 156 rows containing non-finite values (stat_summary). 
8: Removed 156 rows containing non-finite values (stat_summary). 
Execution halted
```

In the Terminal, I type

```{r}
kpsewhich --all --engine=pdftex pdflatex.fmt
```

And get:

```{r}
(base) administrators-MacBook-3:Lifetime Project danielapalleschi$ kpsewhich --all --engine=pdftex pdflatex.fmt
/usr/local/texlive/2019/texmf-var/web2c/pdftex/pdflatex.fmt
```

So my pdftex version is from 2019 or soemthing.

In the Terminal, I typed:

```{r}
sudo fmtutil-sys --all
```

And entered my laptop's administrator Password when prompted (it won't show that you're typing, but just type the password and hit Enter). Then Terminal was running for a couple of minutes, and ended with the output:

```{r}
Transcript written on mex.log.
fmtutil [INFO]: /usr/local/texlive/2019/texmf-var/web2c/pdftex/mex.fmt installed.
fmtutil [INFO]: Disabled formats: 6
fmtutil [INFO]: Successfully rebuilt formats: 45
fmtutil [INFO]: Total formats: 51
fmtutil [INFO]: exiting with status 0
```

When I went to knit a PDF I still got the same errors.

Then I ran 

```{r}
fmtutil-user --all
```

No password required, it ran again for a few minutes. At the end, I got

```{r}
Transcript written on mf.log.
fmtutil [INFO]: /Users/danielapalleschi/Library/texlive/2019/texmf-var/web2c/metafont/mf.base installed.
fmtutil [INFO]: Disabled formats: 6
fmtutil [INFO]: Successfully rebuilt formats: 45
fmtutil [INFO]: Total formats: 51
fmtutil [INFO]: exiting with status 0
```

## Solution

In the end I went into my Time Machine (external harddrive) to a point right before this problem started happening, and restored the 'Macintosh HD/Library' folder, because presumably something happened to the TexLive file or something in there. That solved the problem.

# Rmarkdown and kableExtra with R version 4.3

# Stargazer

## Solution


```{r, eval= F, echo = F}
# 31.08.2022: updated to R version 4.2.1 (2022-06-23), now stargazer won't compile tables where model names are too long
# Solution: discussed here https://www.reddit.com/r/rstats/comments/ucmtdn/issue_with_stargazer_package_after_update_to_r_420/
# code below from linked solution https://gist.github.com/alexeyknorre/b0780836f4cec04d41a863a683f91b53
## Quick fix for stargazer <= 5.2.3 is.na() issue with long model names in R >= 4.2
# Unload stargazer if loaded
detach("package:stargazer",unload=T)
# Delete it
remove.packages("stargazer")
# Download the source
download.file("https://cran.r-project.org/src/contrib/stargazer_5.2.3.tar.gz", destfile = "stargazer_5.2.3.tar.gz")
# Unpack
untar("stargazer_5.2.3.tar.gz")
# Read the sourcefile with .inside.bracket fun
stargazer_src <- readLines("stargazer/R/stargazer-internal.R")
# Move the length check 5 lines up so it precedes is.na(.)
stargazer_src[1990] <- stargazer_src[1995]
stargazer_src[1995] <- ""
# Save back
writeLines(stargazer_src, con="stargazer/R/stargazer-internal.R")
# Compile and install the patched package
install.packages("stargazer", repos = NULL, type="source")
### FROM NOW ON: shorter model names for stargazer

`